/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.opensource.svgaplayer.utils;

import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import java.math.BigDecimal;

/**
 * ex.
 *
 * @author wl
 * @since 2021-06-28
 */
public class PixelUtil {
    private static Display display;

    /**
     * initContext
     *
     * @param context Context
     */
    public static void initContext(Context context) {
        display = DisplayManager.getInstance().getDefaultDisplay(context).get();
    }

    /**
     * screenWidth
     *
     * @return int
     */
    public static int screenWidth() {
        return display.getAttributes().width;
    }

    /**
     * screenHeight
     *
     * @return int
     */
    public static int screenHeight() {
        return display.getAttributes().height;
    }

    /**
     * fp2px
     *
     * @param fpValue float
     * @return float
     */
    public static float fp2px(float fpValue) {
        float sca = display.getAttributes().scalDensity;
        BigDecimal bigDecimal1 = new BigDecimal(fpValue).multiply(new BigDecimal(sca));
        BigDecimal bigDecimal2 = new BigDecimal(0.5F).multiply(new BigDecimal(fpValue >= 0 ? 1 : -1));
        return bigDecimal1.add(bigDecimal2).floatValue();
    }

    /**
     * vp2px
     *
     * @param vpValue float
     * @return float
     */
    public static float vp2px(float vpValue) {
        float dpi = display.getAttributes().densityPixels;
        BigDecimal bigDecimal1 = new BigDecimal(vpValue).multiply(new BigDecimal(dpi));
        BigDecimal bigDecimal2 = new BigDecimal(0.5F).multiply(new BigDecimal(vpValue >= 0 ? 1 : -1));
        return  bigDecimal1.add(bigDecimal2).floatValue();
    }

    /**
     * getMax
     *
     * @param radius int...
     * @return int
     */
    public static int getMax(int... radius) {
        if (radius.length == 0) {
            return 0;
        }
        int max = radius[0];
        for (int m : radius) {
            if (m > max) {
                max = m;
            }
        }
        return max;
    }

    /**
     * red
     *
     * @param color int
     * @return int
     */
    public static int red(int color) {
        return (color >> 16) & 0xFF;
    }

    /**
     * green
     *
     * @param color int
     * @return int
     */
    public static int green(int color) {
        return (color >> 8) & 0xFF;
    }

    /**
     * blue
     *
     * @param color int
     * @return int
     */
    public static int blue(int color) {
        return color & 0xFF;
    }


}
